package cn.itcast.servlet;

import cn.itcast.datasource.MyDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Demo {
    public static void main(String[] args) throws Exception {
        MyDataSource dataSource = new MyDataSource();
        System.out.println("使用之前连接池数量：" + dataSource.getSize());
        //获取连接对象
        Connection con = dataSource.getConnection();

        String sql = "SELECT * FROM student";

        PreparedStatement pst = con.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();

        while(rs.next()) {
            System.out.println(rs.getInt("sid") + "\t" + rs.getString("name") + "\t" + rs.getInt("age") + "\t" + rs.getDate("birthday"));
        }

        //释放资源
        rs.close();
        pst.close();
        //目前的连接对象close方法，是直接关闭连接，而不是将连接归还池中
        con.close();

        System.out.println("使用之后连接池数量：" + dataSource.getSize());
    }
}
